/*
*****************************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2002-2012, Advanced Audio Video Coding Standard, Part II
*
* DISCLAIMER OF WARRANTY
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY.
* The AVS Working Group doesn't represent or warrant that the programs
* furnished here under are free of infringement of any third-party patents.
* Commercial implementations of AVS, including shareware, may be
* subject to royalty fees to patent holders. Information regarding
* the AVS patent policy for standardization procedure is available at
* AVS Web site http://www.avs.org.cn. Patent Licensing is outside
* of AVS Working Group.
*
* The Initial Developer of the Original Code is Video subgroup of AVS
* Workinggroup.
* Contributors: Qin Yu,         Zhichu He,  Weiran Li,    Yong Ling,
*               Zhenjiang Shao, Jie Chen,   Junjun Si,    Xiaozhen Zheng, 
*               Jian Lou,       Qiang Wang, Jianwen Chen, Haiwu Zhao,
*               Guoping Li,     Siwei Ma,   Junhao Zheng, Zhiming Wang
*               Li Zhang,
******************************************************************************
*/



/*
*************************************************************************************
* File name: commonStructures.h
* Function:  common structures definitions for for AVS encoder and decoder.
*
*************************************************************************************
*/

#ifndef _TYPES_H_
#define _TYPES_H_

#include <stdio.h>                              //!< for FILE
#include "defines.h"

#ifdef WIN32
#define  snprintf _snprintf
#endif

#if EXTEND_BD
typedef unsigned short byte;    //!< byte type definition
#else
typedef unsigned char byte;    //!< byte type definition
#endif
#define pel_t byte

//! Boolean Type
typedef enum
{
  FALSE,
  TRUE
} Boolean;

typedef enum
{
  FRAME_CODING,
  FIELD_CODING,
} /*CodingType*/;

typedef enum
{
  FIELD,
  FRAME
};

typedef enum
{
  UVLC,
  AEC
};

typedef enum
{
  SE_HEADER,
  SE_PTYPE,
  SE_MBTYPE,
  SE_REFFRAME,
  SE_INTRAPREDMODE,
  SE_MVD,
#if DMH
  SE_DMH,
#endif
#if WPM_ACE_OPT
  SE_WPM1,
  SE_WPM2,
#endif
  SE_CBP_INTRA,
  SE_LUM_DC_INTRA,
  SE_CHR_DC_INTRA,
  SE_LUM_AC_INTRA,
  SE_CHR_AC_INTRA,
  SE_CBP_INTER,
  SE_CBP01,
  SE_LUM_DC_INTER,
  SE_CHR_DC_INTER,
  SE_LUM_AC_INTER,
  SE_CHR_AC_INTER,
  SE_DELTA_QUANT_INTER,
  SE_DELTA_QUANT_INTRA,
  SE_BFRAME,
  SE_EOS,
  SE_MAX_ELEMENTS  //!< number of maximum syntax elements, this MUST be the last one!
} /*SE_type*/;

typedef enum
{
  BITS_HEADER,
  BITS_TOTAL_MB,
  BITS_MB_MODE,
  BITS_INTER_MB,
  BITS_CBP_MB,
  BITS_CBP01_MB,
  BITS_COEFF_Y_MB,
  BITS_COEFF_UV_MB,
  BITS_DELTA_QUANT_MB,
  BITS_SAO_MB,
  MAX_BITCOUNTER_MB
} BitCountType;

typedef enum
{
  INTRA_PRED_VER = 0,
  INTRA_PRED_HOR,
  INTRA_PRED_DC_DIAG
};

#define MAX_AMVP_SET_ARRAY 10
typedef struct {
	int pmv_cand[MAX_AMVP_SET_ARRAY][2];
	int pmv_num;
	int pmv_index;
} AMVP_SET;

typedef enum SAOComponentIdx
{
	SAO_Y =0,
	SAO_Cb,
	SAO_Cr,
	NUM_SAO_COMPONENTS
};

typedef enum SAOMode //mode
{
	SAO_MODE_OFF = 0,
	SAO_MODE_MERGE,
	SAO_MODE_NEW,
	NUM_SAO_MODES
};

typedef enum SAOModeMergeTypes
{
	SAO_MERGE_LEFT =0,
	SAO_MERGE_ABOVE,
	NUM_SAO_MERGE_TYPES
};


typedef enum SAOModeNewTypes //NEW: types
{
	SAO_TYPE_EO_0,
	SAO_TYPE_EO_90,
	SAO_TYPE_EO_135,
	SAO_TYPE_EO_45,
	SAO_TYPE_BO ,
	NUM_SAO_NEW_TYPES
};
enum SAOEOClasses // EO Groups, the assignments depended on how you implement the edgeType calculation
{
	SAO_CLASS_EO_FULL_VALLEY = 0,
	SAO_CLASS_EO_HALF_VALLEY = 1,
	SAO_CLASS_EO_PLAIN       = 2,
	SAO_CLASS_EO_HALF_PEAK   = 3,
	SAO_CLASS_EO_FULL_PEAK   = 4,
	SAO_CLASS_BO             = 5, 
	NUM_SAO_EO_CLASSES = SAO_CLASS_BO,
	NUM_SAO_OFFSET
};



typedef struct
{
  int extension_id;
  int copyright_flag;
  int copyright_id;
  int original_or_copy;
  int reserved;
  int copyright_number;
} CopyRight;

typedef struct
{
  int reserved;
  int camera_id;
  int height_of_image_device;
  int focal_length;
  int f_number;
  int vertical_angle_of_view;
  int camera_position_x;
  int camera_position_y;
  int camera_position_z;
  int camera_direction_x;
  int camera_direction_y;
  int camera_direction_z;
  int image_plane_vertical_x;
  int image_plane_vertical_y;
  int image_plane_vertical_z;
} CameraParamters;

//! SNRParameters
typedef struct
{
  double snr_y;               //!< current Y SNR
  double snr_u;               //!< current U SNR
  double snr_v;               //!< current V SNR
  double snr_y1;              //!< SNR Y(dB) first frame
  double snr_u1;              //!< SNR U(dB) first frame
  double snr_v1;              //!< SNR V(dB) first frame
  double snr_ya;              //!< Average SNR Y(dB) remaining frames
  double snr_ua;              //!< Average SNR U(dB) remaining frames
  double snr_va;              //!< Average SNR V(dB) remaining frames
#if INTERLACE_CODING
  double i_snr_ya;               //!< current Y SNR
  double i_snr_ua;               //!< current U SNR
  double i_snr_va;               //!< current V SNR
#endif
} SNRParameters;

// signal to noise ratio parameters
/*struct snr_par
{
  double snr_y;                                //<! current Y SNR
  double snr_u;                                //<! current U SNR
  double snr_v;                                //<! current V SNR
  double snr_y1;                               //<! SNR Y(dB) first frame
  double snr_u1;                               //<! SNR U(dB) first frame
  double snr_v1;                               //<! SNR V(dB) first frame
  double snr_ya;                               //<! Average SNR Y(dB) remaining frames
  double snr_ua;                               //<! Average SNR U(dB) remaining frames
  double snr_va;                               //<! Average SNR V(dB) remaining frames
};*/

//! codingUnit
typedef struct codingUnit
{
  unsigned int        ui_MbBitSize;
  int                 uiBitSize;            //size of MB   
  int                 currSEnr;             //!< number of current syntax element
  int                 slice_nr;
  int                 delta_quant;          //!< for rate control
  int                 delta_qp;
  int                 qp ;
  int                 bitcounter[MAX_BITCOUNTER_MB];
  struct codingUnit   *mb_available[3][3];        /*!< pointer to neighboring MBs in a 3x3 window of current MB, which is located at [1][1] \n
                          NULL pointer identifies neighboring MBs which are unavailable */
  // some storage of codingUnit syntax elements for global access
  int                 cuType;
#if WEIGHTED_SKIP
  int                 weighted_skipmode;
#endif

#if MD_DIRECTSKIP
  int                 md_directskip_mode;
#endif

#if INTRA_2N
  int                 trans_size;
#endif
#if DMH
  int                 mvd[2][BLOCK_MULTIPLE][BLOCK_MULTIPLE][3];          //!< indices correspond to [forw,backw][block_y][block_x][x,y, dmh]
#else
  int                 mvd[2][BLOCK_MULTIPLE][BLOCK_MULTIPLE][2];          //!< indices correspond to [forw,backw][block_y][block_x][x,y]
#endif

  AMVP_SET            predMv_set[2][BLOCK_MULTIPLE][BLOCK_MULTIPLE];
  int                 intra_pred_modes[BLOCK_MULTIPLE*BLOCK_MULTIPLE];
  int                 real_intra_pred_modes[BLOCK_MULTIPLE*BLOCK_MULTIPLE];
  int                 l_ipred_mode; 
  int                 cbp, cbp_blk;
  unsigned long cbp_bits;
   
  int                 b8mode[4];
  int                 b8pdir[4];
  int                 c_ipred_mode;      //!< chroma intra prediction mode


  struct codingUnit   *mb_available_up;   //!< pointer to neighboring MB (AEC)
  struct codingUnit   *mb_available_left; //!< pointer to neighboring MB (AEC)
  int                 mbAddrA, mbAddrB, mbAddrC, mbAddrD;

  int                 slice_set_index;       //!<added by mz, 2008.04
  int                 slice_header_flag;     //added by mz, 2008.04
  int                 sliceqp;         //added by mz, 2008.04
#if MB_DQP
  int                 previouse_qp;
  int                 left_cu_qp;
#endif
#if RPD_ST
  int                 block_available_up;
  int                 block_available_left;
#endif
} codingUnit;

//rm52k_r2


//! all input parameters
typedef struct
{
#if MULTIVIEW_CODING
	//the number of multiview
	int view_number;
	//indicate whether to use depth map
	int bUseDepth;
#endif
  int no_frames;                //!< number of frames to be encoded
  int qpI;                      //!< QP of first frame
  int qpP;                      //!< QP of remaining frames
  int jumpd;                    //!< number of frames to skip in input sequence (e.g 2 takes frame 0,3,6,9...)
#if MULTIVIEW_CODING
  int qpOffset;					//!< QP offset for dependent view and depth
#endif
  int jumpd_all;
  int jumpd_sub[4];
  int hadamard;                 /*!< 0: 'normal' SAD in 1/3 pixel search.  1: use 4x4 Haphazard transform and '
                  Sum of absolute transform difference' in 1/3 pixel search                   */
  int usefme;                   //!< Fast Motion Estimat. 0=disable, 1=UMHexagonS
  int search_range;             /*!< search range - integer pel search and 16x16 blocks.  The search window is
                  generally around the predicted vector. Max vector is 2xmcrange.  For 8x8
                  and 4x4 block sizes the search range is 1/2 of that for 16x16 blocks.       */
  int no_multpred;              /*!< 1: prediction from the last frame only. 2: prediction from the last or
                  second last frame etc.  Maximum 5 frames                                    */
  int img_width;                //!< GH: if CUSTOM image format is chosen, use this size
  int img_height;               //!< GH: width and height must be a multiple of 16 pels
  int yuv_format;               //!< GH: YUV format (0=4:0:0, 1=4:2:0, 2=4:2:2, 3=4:4:4,currently only 4:2:0 is supported)

#if INTERLACE_CODING
  int org_img_width;            //!< The width for the input parameter
  int org_img_height;           //!< The height for the input parameter
  int org_no_frames;            //!< The number of the coding frames for the input parameter
  int output_merged_picture;
#endif

  int  infile_header;           //!< If input file has a header set this to the length of the header
#if MULTIVIEW_CODING
  char infile[MAX_VIEW][1000];             //!< YUV 4:2:0 input format
  char ReconFile[MAX_VIEW][1000];          //!< Reconstructed Pictures
  char depth_infile[MAX_VIEW][1000];             //!< YUV 4:2:0 input format
  char depth_ReconFile[MAX_VIEW][1000];          //!< Reconstructed Pictures
#else
  char infile[1000];            //!< YUV 4:2:0 input format
  char ReconFile[1000];         //!< Reconstructed Pictures
#endif
#if ROI_M3264
  char infile_data[1000];       //!< input ROI data...  
  int ROI_Coding;
#endif
  char outfile[1000];           //!< AVS compressed output bitstream
  char TraceFile[1000];         //!< Trace Outputs
  int intra_period;

#if DHP
  int fframe_enabled;
#endif

#if FFRAME
  int dhp_enabled;
#endif

#if MH_PSKIP_NEW
  int b_mhpskip_enabled;
#endif

#if DMH
  int  b_dmh_enabled;
#endif

#if SEC_TRANS
  int  b_secT_enabled;
#endif

#if FFRAME
  int wsm_enabled;
#endif

  // B pictures
  int successive_Bframe;        //!< number of B frames that will be used

  int successive_Bframe_all;
  int successive_Bframe_sub[4];

  int Hierarchical_B;
  int qpB;                      //!< QP of B frames

  int InterSearch16x16;
  int InterSearch16x8;
  int InterSearch8x16;
  int InterSearch8x8;

  int InterSearchAMP;

  int rdopt;

  int InterlaceCodingOption;

  //AVS
  int aspect_ratio_information;
  int frame_rate_code;//xfwang  2004.7.28
  //int bit_rate;
  int bit_rate_lower;
  int bit_rate_upper;

  int vec_period;
  int seqheader_period; // Random Access period though sequence header

  int video_format;
  int color_description;
  int color_primaries;
  int transfer_characteristics;
  int matrix_coefficients;
  int hour;
  int minute;
  int second;
  int frame_offset;
  int profile_id;
  unsigned int g_uiMaxSizeInBit;
  int level_id;
  int progressive_sequence;
  int repeat_first_field;
  int top_field_first;
  int low_delay;
  int chroma_format;
  int yuv_structure;
  int sample_precision;
  int video_range;
  int progressive_frame;
  int fixed_picture_qp;
  int time_code_flag;
  int display_horizontal_size;
  int display_vertical_size;

#if M3472
  int TD_mode;
  int view_packing_mode;
  int view_reverse;
#endif

  //  int slice_enable;
  int slice_parameter;
  int slice_row_nr;
  int output_enc_pic;  //output_enc_pic
  int loop_filter_disable;
  int loop_filter_parameter_flag;
  int alpha_c_offset;
  int beta_offset;
#if M3480_TEMPORAL_SCALABLE
  int TemporalScalableFlag;
#endif
  int sao_enable;
#if ALF
  int alf_enable;
  int alf_LowLatencyEncodingEnable;
#endif

#if XL_ADDED
  int crossSliceLoopFilter;
#endif

  int channel_type;
  int frame_rate;
  int stuff_height;

  int  slice_set_enable;
  char slice_set_config[1000];
  int  use_rdoq;
  int  lambda_factor_rdoq;
  int  lambda_factor_rdoq_p;
  int  lambda_factor_rdoq_b;
  int  b_pmvr_enabled;

  int  useNSQT;
#if MB_DQP
  int useDQP;
#endif
  int  useSDIP;
#if ADAPTIVE_QUANTIZATION
  int use_adaptiveQP;
#endif

#if REFINED_QP
  int use_refineQP;
#endif

  // Adaptive frequency weighting quantization
#if FREQUENCY_WEIGHTING_QUANTIZATION
#define WQMODEL_PARAM_SIZE 64		//M2148 2007-09
#define FILE_NAME_SIZE 100	        //M2331 2008-04
#endif

#if FREQUENCY_WEIGHTING_QUANTIZATION

  int  WQEnable;
  int  SeqWQM;
  char SeqWQFile[256];

  int PicWQEnable;
  int WQParam;
  int WQModel;
  char WeightParamDetailed[WQMODEL_PARAM_SIZE];			//M2148 2007-09
  char WeightParamUnDetailed[WQMODEL_PARAM_SIZE];			//M2148 2007-09
  int MBAdaptQuant;						//M2331 2008-04
  //#if CHROMA_DELTA_QP
  int chroma_quant_param_disable;
  int chroma_quant_param_delta_u;
  int chroma_quant_param_delta_v;
  //#endif
  int PicWQM;
  char PicWQFile[256];
#endif

  char bbv_mode;
  int  bbv_buffer_size;

#ifdef AVS2_S2_BGLONGTERM
  int  bg_enable;

  char bg_file_name[1000];
  int  bg_input_number;
  int  bg_period;
  int  bg_model_number;
  int  bg_qp;
  int  bg_model_method;
  int  always_no_bgmodel;

#if AVS2_S2_FASTMODEDECISION
  int   bg_fast_mode;
#endif

#endif

#if EXTEND_BD
	int   input_sample_bit_depth; // inputfile bit depth
  int   sample_bit_depth;  // sample bit depth
#endif

#ifdef TDRDO
  int TDEnable;
#endif
#ifdef RATECONTROL
  int EncControl;
  int TargetBitRate;
  int ECInitialQP;
#endif
} InputParameters;


// image parameters
typedef struct syntaxelement SyntaxElement;
typedef struct slice Slice;
typedef struct SAOstatdata SAOStatData;
#if ALF
typedef struct alfdatapart AlfDataPart;
#endif
typedef struct  
{
	int modeIdc; //NEW, MERGE, OFF
	int typeIdc; //NEW: EO_0, EO_90, EO_135, EO_45, BO. MERGE: left, above
	int startBand; //BO: starting band index
  int startBand2;
#if DELTA_BAND
  int deltaband;
#endif
	int offset[MAX_NUM_SAO_CLASSES];
}SAOBlkParam;
#if ALF
typedef struct
{
	int alf_flag; 
	int num_coeff; 
	int filters_per_group;
	int componentID;
	int *filterPattern;
	int **coeffmulti;
}ALFParam;

enum ALFComponentID
{
	ALF_Y = 0,
	ALF_Cb,
	ALF_Cr,
	NUM_ALF_COMPONENT
};
typedef struct
{
	int usedflag;
	int cur_number;
	int max_number;
	ALFParam alf_par[NUM_ALF_COMPONENT];
}ALF_APS;

#endif
typedef struct
{
  codingUnit    *mb_data; 
  int number;                                 //<! frame number
  int numIPFrames;

#if MULTIVIEW_CODING
  int view_index;
  int baseView_type;
#endif

  int type;
#ifdef AVS2_S2_BGLONGTERM
  int typeb;
  int typeb_before;
#endif
  int qp;                                     //<! quant for the current frame
  int current_mb_nr;              // bitstream order
  int current_slice_nr;
  int tr;                                     //<! temporal reference, 8 bit, 

  int width;                   //!< Number of pels
  int width_cr;                //!< Number of pels chroma
  int height;                  //!< Number of lines
  int height_cr;               //!< Number of lines  chroma
  int PicWidthInMbs;
  int PicSizeInMbs;
  int block8_x, block8_y;
  int   subblock_x;
  int   subblock_y;

#if WEIGHTED_SKIP
  int num_of_references;
#endif

  int auto_crop_right;                        //<! Bug Fix: correct picture size for outputted reconstructed pictures
  int auto_crop_bottom;
  int buf_cycle;
  int picture_structure;
  Slice       *currentSlice;                  //<! pointer to current Slice data struct

  int **predBlock;             //!< current best prediction mode
  int **resiY;              //!< the diff pixel values between orginal image and prediction
  int *quad;               //!< Array containing square values,used for snr computation    

  //////////////////////////////////////////////////////////////////////////
  /////////////////////location of current MB////////////////////////////
  //////////////////////////////////////////////////////////////////////////
  int mb_y;                    //!< current MB vertical
  int mb_x;                    //!< current MB horizontal
  int pix_y;                   //!< current pixel vertical
  int pix_x;                   //!< current pixel horizontal
  int pix_c_y;                 //!< current pixel chroma vertical
  int pix_c_x;                 //!< current pixel chroma horizontal

  int imgtr_next_P;

#if MULTIVIEW_CODING
  int multiView_imgtr_fwRefDistance[MAX_VIEW][REF_MAXBUFFER];
  int multiView_imgcoi_ref[MAX_VIEW][REF_MAXBUFFER];
  int multiView_refered_by_others[MAX_VIEW][REF_MAXBUFFER];
  int multiView_depth_imgtr_fwRefDistance[MAX_VIEW][REF_MAXBUFFER];
  int multiView_depth_imgcoi_ref[MAX_VIEW][REF_MAXBUFFER];
  int multiView_depth_refered_by_others[MAX_VIEW][REF_MAXBUFFER];
  int ***multiView_depth_tmp_mv[MAX_VIEW];
  int ***multiView_depth_fw_mv[MAX_VIEW];
  int ***multiView_depth_bw_mv[MAX_VIEW];
  int **multiView_depth_fw_refFrArr[MAX_VIEW];                          //<! [72][88];
  int **multiView_depth_bw_refFrArr[MAX_VIEW];                          //<! [72][88];
  int bDepth;
  int currView_imgtr_fwRefDistance[REF_MAXBUFFER];
  int currView_imgcoi_ref[REF_MAXBUFFER];
  int currView_refered_by_others[REF_MAXBUFFER];
#endif

  int imgtr_fwRefDistance[REF_MAXBUFFER];
  int imgcoi_ref[REF_MAXBUFFER];
#if !MULTIVIEW_CODING
  int imgcoi_next_ref;
#endif
  int refered_by_others[REF_MAXBUFFER];
#if M3481_REMOVE_BUGFIX
  int is_output[REF_MAXBUFFER];
#endif

#if MULTIVIEW_CODING
  int multiView_is_output[MAX_VIEW][REF_MAXBUFFER];
  int multiView_depth_is_ouput[MAX_VIEW][REF_MAXBUFFER];
#endif

#if M3480_TEMPORAL_SCALABLE
  int temporal_id[REF_MAXBUFFER];       //temporal level setted in configure file

#endif

  int **ipredmode;             //!< GH ipredmode[90][74];prediction mode for inter frames */ /* fix from ver 4.1


////////////////decoder//////////////////////////
  int max_mb_nr;
  int **intra_block;

  int block_y;
  int block_x;

  int ***tmp_mv;                                  //<! [92][72][3]  // delete by xfwang 2004.7.29

#if MULTIVIEW_CODING
  int ***multiView_tmp_mv[MAX_VIEW];
#endif

#if MH_PSKIP || DHP
  int ***p_snd_tmp_mv;
#endif 
  int resiUV[2][MAX_CU_SIZE][MAX_CU_SIZE];                       //<! final 4x4 block. Extended to 16x16 for AVS

  int **fw_refFrArr;                          //<! [72][88];
  int **bw_refFrArr;                          //<! [72][88];

#if MULTIVIEW_CODING
  int **multiView_fw_refFrArr[MAX_VIEW];                          //<! [72][88];
  int **multiView_bw_refFrArr[MAX_VIEW];                          //<! [72][88];
#endif

#if !M3475_RANDOM_ACCESS
  int no_forward_reference;
#else 
  int random_access_decodable_flag;
#endif

  int seq_header_indicate;
  int B_discard_flag;

#if MULTIVIEW_CODING
  int ***multiView_fw_mv[MAX_VIEW];
  int ***multiView_bw_mv[MAX_VIEW];
#endif
  // B pictures
  int ***fw_mv;                               //<! [92][72][3];
  int ***bw_mv;                               //<! [92][72][3];

  unsigned int pic_distance;

  unsigned int coding_order;

  unsigned int PrevPicDistanceLsb;
#if MULTIVIEW_CODING
  unsigned int multiView_PrevPicDistanceLsb[MAX_VIEW];
#endif
  signed int CurrPicDistanceMsb;

  int PicHeightInMbs;

  int types;

  int new_sequence_flag;
  int sequence_end_flag;            //<! rm52k_r2

  int current_slice_set_index;          //<! added by mz, 2008.04
  int current_slice_header_flag;        //<! added by mz, 2008.04
  int slice_set_qp[64];             //<! added by mz, 2008.04


  int inter_amp_enable;

//////////////////////////encoder//////////////////////////

  int nb_references;

  int framerate;

  int ***predBlockY;         //!< all 9 prediction modes
  int ****predBlockUV;      //!< new chroma 8x8 intra prediction modes
  
  int **Coeff_all;//qyu 0821

  SyntaxElement   *MB_SyntaxElements; //!< by oliver 0612
                          
  // B pictures

  int b_frame_to_code;
  int num_ref_pic_active_fwd_minus1;
  int num_ref_pic_active_bwd_minus1;
  int mv_range_flag;

  unsigned int frame_num;   //frame_num for this frame
#if LCUBASE_SLICE
  int slice_offset;
#endif
  //the following are sent in the slice header
  int NoResidueDirect;
  int coded_mb_nr;
  int progressive_frame;
#if !M3452_TC
  int dropflag;
#else 
  int tc_reserve_bit;
#endif 
  int mb_no_currSliceLastMB; // the last MB no in current slice.      Yulj 2004.07.15
  int Seqheader_flag;     // Added by cjw, 20070327
  int EncodeEnd_flag;         // Carmen, 2007/12/19

  unsigned short bbv_delay;

  int***** allSymMv;       //!< replaces local allSymMv
  int***** allFwMv;       //!< replaces local allFwMv
  int***** allBwMv;      //!< replaces local allBwMv
#if DHP_OPT
  int***** forwardpred_mv;
  int***** forwardpred_allFwMv;
#endif
  int***** allBidFwMv;
  int***** allBidBwMv;

  int***** allIntegerBFwMv;
  int***** allIntegerBBwMv;
  int***** allIntegerPFwMv;

  int***** mv;                 //!< motion vectors for all block types and all reference frames
  int***** predBFwMv;       //!< for MVDFW
  int***** predBBwMv;       //!< for MVDBW
  int***** predSymMv  ;//!< for MVD Bid

  int***** predBidFwMv;
  int***** predBidBwMv;

#if DHP
  int***** predDualFstMv;    //first motion vector predictor of dual hypothesis prediction mode
  int***** predDualSndMv;    //second motion vector predictor of dual hypothesis prediction mode

  int***** allDualFstMv;    //first motion vector of dual hypothesis prediction mode
  int***** allDualSndMv;    //second motion vector of dual hypothesis prediction mode
#endif

#if MD_DIRECTSKIP
  int tmp_fwBSkipMv[DIRECTION+1][2];
  int tmp_bwBSkipMv[DIRECTION+1][2];
#endif

#if MH_PSKIP
  int tmp_pref_fst[MH_PSKIP_NUM+NUM_OFFSET+1];
  int tmp_pref_snd[MH_PSKIP_NUM+NUM_OFFSET+1];
  int tmp_fstPSkipMv[MH_PSKIP_NUM+NUM_OFFSET+1][3];
  int tmp_sndPSkipMv[MH_PSKIP_NUM+NUM_OFFSET+1][3];
#endif
  AMVP_SET***** predPMv_set;
  AMVP_SET***** predBFwMv_set;
  AMVP_SET***** predBBwMv_set;
  AMVP_SET***** predSymMv_set;
  AMVP_SET***** predBidFwMv_set;
  AMVP_SET***** predBidBwMv_set;
  int ***currMv;
  int ***currFwMv;
  int ***currBwMv;
  int **currRef;
  int **currFwRef;
  int **currBwRef;
#if MH_PSKIP || DHP
  int **currPSndRef;
  int ***currPSndMv;
#endif

  int **ipredmode_curr;
  int **Coeff_all_to_write; 
  byte **recon_currY , **recon_currU, **recon_currV;
  ////////////////SAO parameter//////////////////
  SAOStatData*** saostatData;  //[SMB][comp][types]
  SAOBlkParam**  saoBlkParams; //[SMB][comp]  
  SAOBlkParam** rec_saoBlkParams;//[SMB][comp]
  double* cur_saorate;

#if MULTIVIEW_CODING
  double* multiView_cur_saorate[MAX_VIEW];
  double* multiView_depth_cur_saorate[MAX_VIEW];
#endif

  int slice_sao_on[NUM_SAO_COMPONENTS];
#if ALF
  int pic_alf_on[NUM_ALF_COMPONENT];
#if !ALF_BUFFER_OFF
  int pic_alf_apsId;
#endif
#if !ALF_BUFFER_OFF
  int pic_alf_apsNewflag;
#endif
  int		***Coeff_all_to_write_ALF;
  AlfDataPart *dp_ALF;
#endif
#if INTERLACE_CODING
  int is_field_sequence;
  int is_top_field;
#endif
} ImageParameters;



//! struct for context management
typedef struct
{
  unsigned char MPS;   //1 bit
  unsigned int  LG_PMPS; //10 bits
  unsigned char cycno;  //2 bits
} BiContextType;
typedef BiContextType *BiContextTypePtr;

/***********************************************************************
* D a t a    t y p e s   f o r  A E C
************************************************************************/



typedef struct pix_pos
{
  int available;   //ABCD
  int mb_addr;    //MB position
  int x;
  int y;
  int pos_x;     //4x4 x-pos
  int pos_y;
} PixelPos;



typedef struct
{
	int type;
#ifdef AVS2_S2_BGLONGTERM
	int typeb;
#endif
  int   framenum;
  int   tr;
  int   qp;
  double snr_y;
  double snr_u;
  double snr_v;
  int   tmp_time;
  int   picture_structure;
  int   curr_frame_bits;
  int   emulate_bits;
} STDOUT_DATA;

/**********************************************************************
* C O N T E X T S   F O R   T M L   S Y N T A X   E L E M E N T S
**********************************************************************
*/
#define NUM_CuType_CTX              11+10
#define NUM_B8_TYPE_CTX              9
#define NUM_MVD_CTX                 10
#define NUM_PMV_IDX_CTX             10        
#define NUM_REF_NO_CTX               6
#define NUM_DELTA_QP_CTX             4
#define NUM_INTER_DIR_CTX           18
#if DHP
#define NUM_INTER_DIR_DHP_CTX           3
#define NUM_B8_TYPE_DHP_CTX             1
#endif
#define NUM_AMP_CTX                  2
#define NUM_C_INTRA_MODE_CTX         4
#define NUM_CBP_CTX                  4
#define NUM_BCBP_CTX                 4
#if BBRY_CU8
#define NUM_MAP_CTX                 17
#define NUM_LAST_CTX                17
#else
#define NUM_MAP_CTX                 11
#define NUM_LAST_CTX                11
#endif

#define NUM_INTRA_MODE_CTX           7

#define NUM_ABS_CTX                  5
#if M3547_REMOVE_INTRA_NXN
#define NUM_TU_CTX                   3
#else
#define NUM_TU_CTX                   2
#endif
#define NUM_SPLIT_CTX                8  // CU depth

#ifdef AVS2_S2_SBD
#define NUM_BRP_CTX					 8
#endif

#define NUM_LAST_CG_CTX_LUMA        12 
#define NUM_LAST_CG_CTX_CHROMA       6
#define NUM_SIGCG_CTX_LUMA           2
#define NUM_SIGCG_CTX_CHROMA         1
#if BBRY_CU8
#define NUM_LAST_POS_CTX_LUMA   56
#define NUM_LAST_POS_CTX_CHROMA 16
#else
#define NUM_LAST_POS_CTX_LUMA   60
#define NUM_LAST_POS_CTX_CHROMA 36
#endif
#define NUM_LAST_CG_CTX             (NUM_LAST_CG_CTX_LUMA+NUM_LAST_CG_CTX_CHROMA)
#define NUM_SIGCG_CTX               (NUM_SIGCG_CTX_LUMA+NUM_SIGCG_CTX_CHROMA)
#define NUM_LAST_POS_CTX        (NUM_LAST_POS_CTX_LUMA+NUM_LAST_POS_CTX_CHROMA)
#define NUM_SAO_MERGE_FLAG_CTX                   3
#define NUM_SAO_MODE_CTX                         1
#define NUM_SAO_OFFSET_CTX                       2
#if SIMP_MV
#define NUM_INTER_DIR_MIN_CTX         2
#endif

typedef struct
{
  BiContextType cuType_contexts       [NUM_CuType_CTX];
  BiContextType pdir_contexts         [NUM_INTER_DIR_CTX];
  BiContextType amp_contexts          [NUM_AMP_CTX];
  BiContextType b8_type_contexts      [NUM_B8_TYPE_CTX];
#if DHP
  BiContextType pdir_dhp_contexts         [NUM_INTER_DIR_DHP_CTX];
  BiContextType b8_type_dhp_contexts      [NUM_B8_TYPE_DHP_CTX];
#endif
#if MD_DIRECTSKIP
  BiContextType b_dir_skip_contexts   [DIRECTION];
#endif
#if MH_PSKIP
  BiContextType p_skip_mode_contexts  [MH_PSKIP_NUM];
#endif

#if WPM_ACE_OPT
  BiContextType wpm_contexts[WPM_NUM];
#endif

#if DMH
  BiContextType mvd_contexts          [3][NUM_MVD_CTX];
#else
  BiContextType mvd_contexts          [2][NUM_MVD_CTX];
#endif
  BiContextType pmv_idx_contexts      [2][NUM_PMV_IDX_CTX]; 
  BiContextType ref_no_contexts       [NUM_REF_NO_CTX];
  BiContextType delta_qp_contexts     [NUM_DELTA_QP_CTX];
  BiContextType l_intra_mode_contexts [NUM_INTRA_MODE_CTX];
  BiContextType c_intra_mode_contexts [NUM_C_INTRA_MODE_CTX];
  BiContextType cbp_contexts          [3][NUM_CBP_CTX];  
  BiContextType map_contexts          [NUM_BLOCK_TYPES][NUM_MAP_CTX];
  BiContextType last_contexts         [NUM_BLOCK_TYPES][NUM_LAST_CTX];
  BiContextType split_contexts        [NUM_SPLIT_CTX];
  BiContextType tu_contexts           [NUM_TU_CTX];
  BiContextType lastCG_contexts       [NUM_LAST_CG_CTX];
  BiContextType sigCG_contexts        [NUM_SIGCG_CTX];
  BiContextType lastPos_contexts[NUM_LAST_POS_CTX];
  BiContextType saomergeflag_context     [NUM_SAO_MERGE_FLAG_CTX];
  BiContextType saomode_context          [NUM_SAO_MODE_CTX];
  BiContextType saooffset_context          [NUM_SAO_OFFSET_CTX];
#if ALF
#if !ALF_COEFF_VLC
  BiContextType m_cALF_COEFF_SCModel        [NUM_ALF_COEFF_CTX];
#endif
  BiContextType m_cALFLCU_Enable_SCModel    [3][NUM_ALF_LCU_CTX];
#endif
#ifdef AVS2_S2_SBD
  BiContextType brp_contexts          [NUM_BRP_CTX];
#endif
#if SIMP_MV
  BiContextType pdirMin_contexts      [NUM_INTER_DIR_MIN_CTX];
#endif
} SyntaxInfoContexts;

#endif // #ifndef _TYPES_H_

